package com.enation.app.shop.front.api.appcontroller;

import com.enation.app.base.core.model.Member;
import com.enation.app.base.core.service.IMemberManager;
import com.enation.app.shop.front.api.credit.entity.CreditAccount;
import com.enation.app.shop.front.api.credit.service.ICreditAccountManager;
import com.enation.app.shop.front.api.jpush.service.IJPushManager;
import com.enation.app.shop.front.api.random.IMobileRandomManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;
import java.util.Timer;

@Scope("prototype")
@Controller
@RequestMapping("/app/time")
public class TimeController {
    @Autowired
    private IMobileRandomManager mobileRandomManager;
    @Autowired
    private ICreditAccountManager creditAccountManager;
    @Autowired
    private IJPushManager jPushManager;
    @Autowired
    private IMemberManager memberManager;
    private Logger logger = Logger.getLogger(getClass());


    /*
    * 还款提醒每小时执行一次
    * */
    public void everyHourApp(){
      List<CreditAccount> creditAccountList=creditAccountManager.getAll();
      for (CreditAccount creditAccount:creditAccountList){
          if (creditAccount.getProvisional_maturity()==null){
              continue;
          }else {
              long promaturity=creditAccount.getProvisional_maturity().getTime();//临时额度到期时间
              long now=new Date().getTime();
              //System.out.println(now);
              long oneday=1000*60*60*24;
              long temp=promaturity-now;
              if (temp<oneday){
                   Integer userid=creditAccount.getUser_id();
                   Member member=memberManager.getapp(userid);
                   try {
                       String jpushrid=member.getJpushregisid();
                       jPushManager.JpushTest(userid,jpushrid,2);
                   }catch (Exception e){
                        logger.error("jpushrid is null");
                   }
              }
          }
      }
    }




    @Scheduled(fixedRate=1000)
    public void Timetest(){
        Timer timer=new Timer();
        //timer.schedule(new  TimerTask(), 3000);
        //System.out.println("schletest");
    }
















}
